package com.mdc.mansion.web.modules.restaurant.web;

import com.mdc.mansion.core.modules.restaurant.RestaurantService;
import com.mdc.mansion.dataaccess.modules.restaurant.entity.Restaurant;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.mdc.mansion.web.modules.system.utils.UserUtils;
import com.mdc.mansion.dataaccess.modules.system.entity.User;

import com.mdc.mansion.dataaccess.common.persistence.Page;
import com.mdc.mansion.dataaccess.modules.restaurant.entity.RestaurantTable;
import com.mdc.mansion.core.modules.restaurant.RestaurantTableService;
import com.mdc.mansion.web.common.web.BaseController;
/**
 * 餐厅餐桌信息Controller
 * @author mingdongc
 * @version 2018-01-01
 */
@Controller
@RequestMapping(value = "${adminPath}/restaurant/restaurantTable")
public class RestaurantTableController extends BaseController {

	@Autowired
	private RestaurantTableService restaurantTableService;

	@Autowired
	private RestaurantService restaurantService;
	
	@ModelAttribute
	public RestaurantTable get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return restaurantTableService.get(id);
		}else{
			return new RestaurantTable();
		}
	}
	
	@RequiresPermissions("restaurant:restaurantTable:view")
	@RequestMapping(value = {"list", ""})
	public ModelAndView list(RestaurantTable restaurantTable) {
		Map<String, Object> model=new HashMap<String, Object>();
		Page<RestaurantTable> page = restaurantTableService.findPage(restaurantTable);
        model.put("page", page);
		formatRestaurantTable(page.getList());
		List<Restaurant> list = restaurantService.findList(new Restaurant());
		model.put("restaurants",list);
		ModelAndView modelAndView=new ModelAndView("modules/restaurant/restaurantTableList", model);
		return modelAndView;
	}

	private void formatRestaurantTable(List<RestaurantTable> list) {
		Map<String,String> restaurantMap=new HashMap<>();
		if(CollectionUtils.isEmpty(list)){return;}
		for(RestaurantTable restaurantTable:list){
			String restaurantId = restaurantTable.getRestaurantId();
			if(!restaurantMap.containsKey(restaurantId)){
				Restaurant restaurant = restaurantService.get(restaurantId);
				if(restaurant==null){restaurantMap.put(restaurantId,"");}else{
					restaurantMap.put(restaurantId,restaurant.getName());
				}
			}
			restaurantTable.setRestaurantName(restaurantMap.get(restaurantId));
		}
	}

	@RequiresPermissions("restaurant:restaurantTable:view")
	@RequestMapping(value = "form")
	public ModelAndView form(RestaurantTable restaurantTable) {
		Map<String, Object> model=new HashMap<String, Object>();
		model.put("restaurantTable", restaurantTable);
		return new ModelAndView("modules/restaurant/restaurantTableForm",model);
	}
	

	@RequiresPermissions("restaurant:restaurantTable:edit")
	@RequestMapping(value = "save")
	public ModelAndView save(RestaurantTable restaurantTable,RedirectAttributes redirectAttributes) {
		User u=UserUtils.getUser();
		restaurantTable.setUpdateBy(u);
		if (StringUtils.isNotBlank(restaurantTable.getId())){
			restaurantTableService.updateSelective(restaurantTable);
		}else{
			restaurantTable.setCreateBy(u);
			restaurantTableService.save(restaurantTable);
		}
		this.addMessage(redirectAttributes, "操作成功");
		return new ModelAndView("redirect:./list");
	}
	
	@RequiresPermissions("restaurant:restaurantTable:edit")
	@RequestMapping(value = "delete")
	public ModelAndView delete(RestaurantTable restaurantTable,RedirectAttributes redirectAttributes) {
		restaurantTableService.delete(restaurantTable);
		this.addMessage(redirectAttributes, "操作成功");
		return new ModelAndView("redirect:./list");
	}
	
}
